package com.lupus.business;

import java.util.*;
import java.sql.*;

import com.lupus.common.*;
import com.lupus.persistence.*;

public final class CommonBusiness extends Business {
	public Map<String,Object> concreatePerform(Map<String,Object> requestMap,Map<String,Object> responseMap,Connection connection)throws Exception{
		Map<String,Object> actionMap = (Map)requestMap.get("requestAction");
		List<Map> titleList = getTitleList(actionMap,connection);
		List<Map> fieldList = Persistence.getConcretatePersistence(actionMap).operate(actionMap,connection);
		List<Map> footerList = getFooterList(actionMap,connection);
		responseMap.put("dataValues",fieldList);
		responseMap.put("titleValues",titleList);
		responseMap.put("footerValues",footerList);
		return responseMap;
	}
	
	private List<Map> getTitleList(Map<String,Object> actionMap,Connection connection)throws SQLException{
		Object templateId = actionMap.get("templateId");
		Object action = actionMap.get("action");
		if(templateId==null||"".equals(templateId)||!"query".equals(action)){
			return null;
		}
		String currentUserId = (String)actionMap.get("currentUser");
		StringBuffer fieldBuffer = new StringBuffer(512);
		StringBuffer joinBuffer = new StringBuffer(256);
		StringBuffer tableBuffer = new StringBuffer(256);
		Map<String,Object> queryActionMap = CommonFactory.buildAction("query","BAS_TABLEFIELD.*,BAS_TEMPLATEFIELD.ISHIDDEN,BAS_TEMPLATEFIELD.CONTROLTYPE","BAS_TABLEFIELD","INNER JOIN BAS_TEMPLATEFIELD ON BAS_TABLEFIELD.TABLENAME=BAS_TEMPLATEFIELD.TABLENAME AND BAS_TEMPLATEFIELD.FIELDNAME=BAS_TABLEFIELD.FIELDNAME INNER JOIN BAS_FIELDRIGHT ON BAS_FIELDRIGHT.TABLENAME=BAS_TEMPLATEFIELD.TABLENAME AND BAS_FIELDRIGHT.FIELDNAME=BAS_TEMPLATEFIELD.FIELDNAME"," BAS_TEMPLATEFIELD.TEMPLATEID='"+templateId+"' AND ((BAS_FIELDRIGHT.USERID='"+currentUserId+"' AND OWNERTYPE='1') OR (BAS_FIELDRIGHT.USERID IN (SELECT ROLEID FROM BAS_USERROLE WHERE USERID='"+currentUserId+"') AND OWNERTYPE='0')) ORDER BY BAS_TEMPLATEFIELD.COLSEQ",(String)actionMap.get("localLanguage"));
		List<Map> result = Persistence.getConcretatePersistence(queryActionMap).operate(queryActionMap,connection);
		int size = result.size();
		Map tempMap = null;
		for(int i=0;i<size;i++){
			tempMap = (Map)result.get(i);
			String tableName = (String)tempMap.get("TABLENAME");
			String fieldName = (String)tempMap.get("FIELDNAME");
			String fieldType = (String)tempMap.get("FIELDTYPE");
			if(tableBuffer.indexOf(tableName)<0){
				tableBuffer.append(","+tableName);
			}
			if(fieldBuffer.indexOf(fieldName)<0){
				fieldBuffer.append(","+tableName+"."+fieldName);
			}
			if("1".equals(fieldType)){
				Object refField = tempMap.get("REFFIELD");
				joinBuffer.append(" LEFT JOIN "+tempMap.get("REFTABLE")+" ALIAS"+i+" ON "+tableName+"."+fieldName+"=ALIAS"+i+"."+refField);
				fieldBuffer.append(",ALIAS"+i+"."+tempMap.get("REFNAME")+" AS "+fieldName+"TEXT");
			}
		}
		actionMap.put("fieldNames",fieldBuffer.substring(1,fieldBuffer.length()));
		actionMap.put("tableName",tableBuffer.substring(1,tableBuffer.length()));
		actionMap.put("joinTables",joinBuffer.toString());
		return result;
	}
	
	private List<Map> getFooterList(Map<String,Object> actionMap,Connection connection)throws SQLException{
		Object firstRecord = actionMap.get("firstRecord");
		Object pageSize = actionMap.get("pageSize");
		String localLanguage = (String)actionMap.get("localLanguage");
		if(firstRecord==null||pageSize==null){
			return null;
		}
		Map<String,Object> queryActionMap = CommonFactory.buildAction("query","COUNT(*) AS ALLSIZE ",(String)actionMap.get("tableName"),(String)actionMap.get("joinTables"),(String)actionMap.get("condition"),localLanguage);
		List<Map> result = Persistence.getConcretatePersistence(queryActionMap).operate(queryActionMap,connection);
		if(result!=null){
			Map tempMap = (Map)result.get(0);
			String footerStyle = (String)actionMap.get("footerStyle");
			if(footerStyle==null){
				footerStyle = "list";
			}
			tempMap.put("footerString",ResourceFactory.getLocalString("common.label.listfooter."+footerStyle, localLanguage));
			tempMap.put("pageSize",pageSize);
			tempMap.put("firstRecord",firstRecord);
		}
		return result;
	}
}
